package cn.com.ddapp.ddm.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.ActivityManager;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.TLog;
import cn.com.ddapp.ddm.widget.TipsToast;
import okhttp3.Call;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2016/7/29 0029.
 */
public class PwdAgainActivity extends BaseActivity {

	@BindView(R.id.register_pwd1)
	EditText pwd1;
	@BindView(R.id.register_pwd2)
	EditText pwd2;

	@Override
	public void initView() {

		setTitle(R.string.forget_password1);
		setTitleLeftImg(R.mipmap.back_icon);

	}

	@Override
	public void initData() {


	}

	@Override
	public int getContentViewId() {
		return R.layout.pwdagain_main;
	}

	@OnClick({R.id.finish_btn})
	void onclick(View v) {

		switch (v.getId()) {

			//完成
			case R.id.finish_btn:

				if (!NetUtils.isConnected(PwdAgainActivity.this)) {
					TipsToast.makeText(PwdAgainActivity.this, R.string.tip_network_error, TipsToast.LENGTH_SHORT).show();
				} else {

					if (TextUtils.isEmpty(pwd1.getText().toString().trim())) {
						TipsToast.makeText(PwdAgainActivity.this, R.string.pwd_is_null, TipsToast.LENGTH_SHORT).show();
						return;
					}
					if (TextUtils.isEmpty(pwd2.getText().toString().trim())) {
						TipsToast.makeText(PwdAgainActivity.this, R.string.confirm_pwd_is_null, TipsToast.LENGTH_SHORT).show();
						return;
					}

					final OkUtilCallback okUtilCallback = new OkUtilCallback() {
						@Override
						public void onMyResponse(String response, int id) {
							TLog.log(getFragmentTag(), response);
							Entity entity = gson.fromJson(response, Entity.class);
							if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
							} else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
								BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
							} else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
								TipsToast.makeText(PwdAgainActivity.this, R.string.successful_update_pwd, TipsToast.LENGTH_SHORT).show();

								ActivityManager.getInstance().finishActivity(ForgetPwdActivity.class);
								ActivityManager.getInstance().finishActivity();
							}
						}

						@Override
						public void onError(Call call, Exception e, int id) {
							super.onError(call, e, id);
							BaseApplication.getIntstance().showToast(R.string.network_error, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
						}
					};
					OKUtilAPI.post(OKUtilAPI.resetPwd, "userName=" + getIntent().getStringExtra(ForgetPwdActivity.LOGIN_EMAIL) + "&pwd=" + pwd1.getText().toString() + "&confirmPwd=" + pwd2.getText().toString() + "&type=1", okUtilCallback);

//					APIRetrofit.getRetrofit()
//							.create(APIService.class)
//							.setPwd(getIntent().getStringExtra(ForgetPwdActivity.LOGIN_EMAIL),
//									pwd1.getText().toString(),
//									pwd2.getText().toString(),
//									1)
//							.subscribeOn(Schedulers.io())
//							.observeOn(AndroidSchedulers.mainThread())
//							.unsubscribeOn(Schedulers.io())
//							.subscribe(new Subscriber<entity>() {
//								@Override
//								public void onCompleted() {
//
//								}
//
//								@Override
//								public void onError(Throwable e) {
//									TipsToast.makeText(PwdAgainActivity.this, R.string.network_error, TipsToast.LENGTH_SHORT).show();
//								}
//
//								@Override
//								public void onNext(entity entity) {
//
//									if (entity.getResult().equals("OK")) {
//										TipsToast.makeText(PwdAgainActivity.this, R.string.successful_update_pwd, TipsToast.LENGTH_SHORT).show();
//
////										SPUtils.setStringData(PwdAgainActivity.this, SPUtils.USERID, String.valueOf(entity.getUserId()));
////										SPUtils.setBooleanData(PwdAgainActivity.this, SPUtils.ISLOGIN, true);
//
////										Intent intent = new Intent(PwdAgainActivity.this, MainActivity.class);
////										startActivity(intent);
//										ActivityManager.getInstance().finishActivity(ForgetPwdActivity.class);
//										ActivityManager.getInstance().finishActivity();
//									} else {
//										TipsToast.makeText(PwdAgainActivity.this, entity.getResult(), TipsToast.LENGTH_SHORT).show();
//									}
//								}
//							});
				}
				break;

		}

	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected String getFragmentTag() {
		return null;
	}
}
